﻿namespace ManagerClinic.HoSoKhamBenh
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Modules;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using System.Linq;
    using ManagerClinic.KhamBenh;
    using ManagerClinic.CanLamSang;
    using ManagerClinic.DonThuoc;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmHoSoKhamBenh : DevExpress.XtraEditors.XtraForm
    {
        DmBenhNhanBusiness dmBenhNhanBusiness = new DmBenhNhanBusiness();
        HoSoKhamBenhNgoaiTruEntity hoSoKhamBenhNgoaiTruEntity = new HoSoKhamBenhNgoaiTruEntity();
        HoSoKhamBenhNgoaiTruBusiness hoSoKhamBenhNgoaiTruBusiness = new HoSoKhamBenhNgoaiTruBusiness();
        DmDichVuBusiness dmDichVuBusiness = new DmDichVuBusiness();
        PhieuKhamBenhBusiness phieuKhamBenhBusiness = new PhieuKhamBenhBusiness();
        ThuPhiDichVuBusiness thuPhiDichVuBusiness = new ThuPhiDichVuBusiness();
        DanhSachDonThuocBusiness danhSachDonThuocBusiness = new DanhSachDonThuocBusiness();
        DonThuocChiTietBusiness donThuocChiTietBusiness = new DonThuocChiTietBusiness();

        IList<DmDichVuEntity> lstDmDichVuEntity = null;
        GridCheckMarksSelection gridCheckMarksSelection = null;

        int State = 0;
        bool isInsertHoSoKhamBenh = false;
        bool isInsertDonThuoc = false;
        string MaBenhNhan = string.Empty;
        DateTime NgayVaoKham = DateTime.Now;

        public frmHoSoKhamBenh(string maBenhNhan)
        {
            InitializeComponent();
            this.MaBenhNhan = maBenhNhan;
            this.LoadMaHoSo();
            gridCheckMarksSelection = new GridCheckMarksSelection(gvDonThuocChiTiet);
        }

        public frmHoSoKhamBenh(HoSoKhamBenhNgoaiTruEntity obj)
        {
            InitializeComponent();
            gridCheckMarksSelection = new GridCheckMarksSelection(gvDonThuocChiTiet);

            hoSoKhamBenhNgoaiTruEntity = hoSoKhamBenhNgoaiTruBusiness.GetHoSoKhamBenhNgoaiTru(obj.MaHoSo);
            this.MaBenhNhan = hoSoKhamBenhNgoaiTruEntity.MaBenhNhan;
            txtMaHoSo.Text = hoSoKhamBenhNgoaiTruEntity.MaHoSo;
            txtNgayVaoKham.Text = hoSoKhamBenhNgoaiTruEntity.NgayVaoKham.ToString("dd/MM/yyyy HH:mm:ss tt");
            this.isInsertHoSoKhamBenh = true;
            this.NgayVaoKham = hoSoKhamBenhNgoaiTruEntity.NgayVaoKham;
        }

        #region "HoSoKhamBenh"
        void LoadDanhDmLoaiDichVu()
        {
            lstDmDichVuEntity = this.dmDichVuBusiness.GetDmDichVuList();
            lkDichVu.Properties.DataSource = lstDmDichVuEntity;
            lkDichVu.Properties.ValueMember = "MaDichVu";
            lkDichVu.Properties.DisplayMember = "TenDichVu";
        }

        private void frmHoSoKhamBenh_Load(object sender, EventArgs e)
        {
            this.LoadThongTinHanhChinh(MaBenhNhan);
            this.LoadDanhDmLoaiDichVu();
            this.LoadHoSoKhamBenh();
            this.LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet(sender, e);
            this.isValidDonThuoc();
            this.LoadDonThuoc(sender, e);
        }

        void LoadMaHoSo()
        {
            txtMaHoSo.Text = ConstantManagerClinic.setCodeManagerClinic("HS");
            txtNgayVaoKham.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss tt");
        }

        public void LoadThongTinHanhChinh(string maBenhNhan)
        {
            var dmBenhNhanEntity = dmBenhNhanBusiness.GetDmBenhNhan(maBenhNhan);
            txtMaBenhNhan.Text = dmBenhNhanEntity.MaBenhNhan;
            txtTenBenhNhan.Text = dmBenhNhanEntity.TenBenhNhan;
            txtNgaySinh.Text = dmBenhNhanEntity.NgaySinh;
            txtThangSinh.Text = dmBenhNhanEntity.ThangSinh;
            txtNamSinh.Text = dmBenhNhanEntity.NamSinh.ToString();
            txtGioiTinh.Text = dmBenhNhanEntity.TenGioiTinh;
            txtDanToc.Text = dmBenhNhanEntity.TenDanToc;
            txtNgheNghiep.Text = dmBenhNhanEntity.TenNgheNghiep;
            txtDiaChi.Text = dmBenhNhanEntity.DiaChi;
            txtNoiLamViec.Text = dmBenhNhanEntity.NoiLamViec;
            txtDienThoai.Text = dmBenhNhanEntity.SoDienThoai;
            txtEmail.Text = dmBenhNhanEntity.Email;
        }

        void LoadHoSoKhamBenh()
        {
            var hoSoKhamBenhEntity = this.hoSoKhamBenhNgoaiTruBusiness.GetHoSoKhamBenhNgoaiTru(txtMaHoSo.Text);
            if (hoSoKhamBenhEntity == null) return;

            txtMaHoSo.Text = hoSoKhamBenhEntity.MaHoSo;
            txtNgayVaoKham.Text = hoSoKhamBenhEntity.NgayVaoKham.ToString("dd/MM/yyyy hh:mm tt");
            txtLyDoKham.Text = hoSoKhamBenhEntity.LyDoVaoKham;
            txtChanDoan.Text = hoSoKhamBenhEntity.ChanDoan;
            txtKetLuan.Text = hoSoKhamBenhEntity.KetLuan;
        }

        void LoadDichVuSuDung()
        {
            var lstDmDichVuDaSuDung = this.phieuKhamBenhBusiness.GetAllPhieuKhamBenhByMaHoSo(txtMaHoSo.Text);
            grdDichVu.DataSource = lstDmDichVuDaSuDung;
        }

        void LoadDmDichVuChiTietSuDung()
        {
            var lstDmDichVuChiTietDaSuDungEntity = thuPhiDichVuBusiness.GetThuPhiDichVuByMaHoSo(txtMaHoSo.Text);
            if (gvDichVu.RowCount <= 0) return;
            var dmDichVuEntity = (PhieuKhamBenhEntity)gvDichVu.GetFocusedRow();
            if (dmDichVuEntity == null) return;
            lstDmDichVuChiTietDaSuDungEntity = lstDmDichVuChiTietDaSuDungEntity.Where(n => n.MaPhieuDichVu == dmDichVuEntity.MaKhamBenh).ToList();
            grdDichVuChiTiet.DataSource = lstDmDichVuChiTietDaSuDungEntity;
        }

        private void LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet(object sender, EventArgs e)
        {
            LoadDichVuSuDung();
            LoadDmDichVuChiTietSuDung();
        }

        private void btnLuuHoSoKhamBenh_Click(object sender, EventArgs e)
        {
            if (this.State == (int)ConstantManagerClinic.TrangThaiHoSo.TaoMoi)
            {
                hoSoKhamBenhNgoaiTruEntity = new HoSoKhamBenhNgoaiTruEntity();
                hoSoKhamBenhNgoaiTruEntity.MaHoSo = txtMaHoSo.Text;
                hoSoKhamBenhNgoaiTruEntity.MaBenhNhan = txtMaBenhNhan.Text;
                hoSoKhamBenhNgoaiTruEntity.NgayVaoKham = NgayVaoKham;
                hoSoKhamBenhNgoaiTruEntity.TrangThaiPhieu = (int)ConstantManagerClinic.TrangThaiKhamBenh.TaoMoi;
                hoSoKhamBenhNgoaiTruEntity.LyDoVaoKham = txtLyDoKham.Text;
                hoSoKhamBenhNgoaiTruEntity.ChanDoan = txtChanDoan.Text;
                hoSoKhamBenhNgoaiTruEntity.KetLuan = txtKetLuan.Text;
                hoSoKhamBenhNgoaiTruEntity.MaXuTri = string.Empty;
                hoSoKhamBenhNgoaiTruEntity.MaBacSy = string.Empty;
                if (isInsertHoSoKhamBenh == false)
                    isInsertHoSoKhamBenh = hoSoKhamBenhNgoaiTruBusiness.InsertHoSoKhamBenhNgoaiTru(hoSoKhamBenhNgoaiTruEntity);
                else
                    hoSoKhamBenhNgoaiTruBusiness.EditHoSoKhamBenhNgoaiTru(hoSoKhamBenhNgoaiTruEntity);
            }
            else if (this.State == (int)ConstantManagerClinic.TrangThaiHoSo.KhamBenh || this.State == (int)ConstantManagerClinic.TrangThaiHoSo.TroLaiPhongKham)
            {
                hoSoKhamBenhNgoaiTruEntity.MaHoSo = txtMaHoSo.Text;
                hoSoKhamBenhNgoaiTruEntity.MaBenhNhan = txtMaBenhNhan.Text;
                hoSoKhamBenhNgoaiTruEntity.LyDoVaoKham = txtLyDoKham.Text;
                hoSoKhamBenhNgoaiTruEntity.ChanDoan = txtChanDoan.Text;
                hoSoKhamBenhNgoaiTruEntity.KetLuan = txtKetLuan.Text;
                hoSoKhamBenhNgoaiTruEntity.MaXuTri = string.Empty;
                hoSoKhamBenhNgoaiTruEntity.MaBacSy = Authenticate.UserName;
                hoSoKhamBenhNgoaiTruBusiness.EditHoSoKhamBenhNgoaiTru(hoSoKhamBenhNgoaiTruEntity);
            }
            MessageBox.Show("Lưu hồ sơ khám bệnh thành công!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
        }

        private void btnChuyenThuPhi_Click(object sender, EventArgs e)
        {
            if (gvDichVu.RowCount <= 0)
            {
                MessageBox.Show("Bạn chưa tạo dịch vụ khám chữa bệnh!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            bool isExist = false;
            for (int i = 0; i < gvDichVu.RowCount; i++)
            {
                var obj = (PhieuKhamBenhEntity)gvDichVu.GetRow(i);
                if (obj.TrangThaiPhieu == (int)ConstantManagerClinic.TrangThaiKhamBenh.TaoMoi)
                {
                    isExist = true;
                    break;
                }
            }

            if (isExist == false)
            {
                MessageBox.Show("Không có phiếu nào có trạng thái là tạo mới nên không thể chuyển thu phí!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn chuyển bệnh nhân đi thu phí?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }
        }

        private void btnKetThucKhamBenh_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn chắc chắn muốn kết thúc khám bệnh?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }
            this.Close();
        }

        private void btnThemDichVu_Click(object sender, EventArgs e)
        {
            if (isInsertHoSoKhamBenh == false)
            {
                MessageBox.Show("Bạn chưa lưu hồ sơ khám bệnh!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            var maDichVu = lkDichVu.EditValue.ToString();
            if (string.IsNullOrEmpty(maDichVu) == true)
            {
                MessageBox.Show("Bạn chưa chọn dịch vụ khám chữa bệnh!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            var dmDichVuEntity = lstDmDichVuEntity.SingleOrDefault(n => n.MaDichVu == maDichVu);
            if (dmDichVuEntity == null) return;

            if (dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.KhamBenh)
            {
                var frm = new frmPhieuKhamBenh(hoSoKhamBenhNgoaiTruEntity, maDichVu);
                frm._savePhieuKhamBenh += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
            else if ((dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.ChuanDoanHinhAnh))
            {
                var frm = new frmPhieuChanDoanHinhAnh((int)ConstantManagerClinic.TrangThaiPhieuCanLamSang.TaoMoi, maDichVu, hoSoKhamBenhNgoaiTruEntity);
                frm._SavePhieuCanLamSang += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
            else if ((dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.SieuAm))
            {
                var frm = new frmPhieuSieuAm((int)ConstantManagerClinic.TrangThaiPhieuCanLamSang.TaoMoi, maDichVu, hoSoKhamBenhNgoaiTruEntity);
                frm._SavePhieuCanLamSang += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
            else if ((dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.ThamDoChucNang))
            {
                var frm = new frmPhieuThamDoChucNang((int)ConstantManagerClinic.TrangThaiPhieuCanLamSang.TaoMoi, maDichVu, hoSoKhamBenhNgoaiTruEntity);
                frm._SavePhieuCanLamSang += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
            else if ((dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.XetNghiem))
            {
                var frm = new frmPhieuXetNghiem((int)ConstantManagerClinic.TrangThaiPhieuCanLamSang.TaoMoi, maDichVu, hoSoKhamBenhNgoaiTruEntity);
                frm._SavePhieuCanLamSang += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
            else if ((dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.PhauThuatThuThuatDiChuyen))
            {
                var frm = new frmPhieuPhauThuatThuThuat((int)ConstantManagerClinic.TrangThaiPhieuCanLamSang.TaoMoi, maDichVu, hoSoKhamBenhNgoaiTruEntity);
                frm._SavePhieuCanLamSang += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
            else if ((dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.PhauThuatThuThuatTaiCho))
            {
                var frm = new frmPhieuPhauThuatThuThuatTaiCho((int)ConstantManagerClinic.TrangThaiPhieuCanLamSang.TaoMoi, maDichVu, hoSoKhamBenhNgoaiTruEntity);
                frm._SavePhieuCanLamSang += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }

        }

        private void grdDichVu_DoubleClick(object sender, EventArgs e)
        {
            if (gvDichVu.RowCount <= 0) return;
            var dmDichVuEntity = (PhieuKhamBenhEntity)gvDichVu.GetFocusedRow();

            if (dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.KhamBenh)
            {
                var frm = new frmPhieuKhamBenh(dmDichVuEntity.MaKhamBenh);
                frm._savePhieuKhamBenh += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
            else if ((dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.ChuanDoanHinhAnh))
            {
                var frm = new frmPhieuChanDoanHinhAnh(dmDichVuEntity.TrangThaiPhieu, dmDichVuEntity.MaKhamBenh);
                frm._SavePhieuCanLamSang += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
            else if ((dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.SieuAm))
            {
                var frm = new frmPhieuSieuAm(dmDichVuEntity.TrangThaiPhieu, dmDichVuEntity.MaKhamBenh);
                frm._SavePhieuCanLamSang += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
            else if ((dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.ThamDoChucNang))
            {
                var frm = new frmPhieuThamDoChucNang(dmDichVuEntity.TrangThaiPhieu, dmDichVuEntity.MaKhamBenh);
                frm._SavePhieuCanLamSang += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
            else if ((dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.XetNghiem))
            {
                var frm = new frmPhieuXetNghiem(dmDichVuEntity.TrangThaiPhieu, dmDichVuEntity.MaKhamBenh);
                frm._SavePhieuCanLamSang += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
            else if ((dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.PhauThuatThuThuatDiChuyen))
            {
                var frm = new frmPhieuPhauThuatThuThuat(dmDichVuEntity.TrangThaiPhieu, dmDichVuEntity.MaKhamBenh);
                frm._SavePhieuCanLamSang += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
            else if ((dmDichVuEntity.MaLoaiDichVu == LoaiPhieu.PhauThuatThuThuatTaiCho))
            {
                var frm = new frmPhieuPhauThuatThuThuatTaiCho(dmDichVuEntity.TrangThaiPhieu, dmDichVuEntity.MaKhamBenh);
                frm._SavePhieuCanLamSang += new EventHandler(LoadDmDichVuSuDungAndDmDichVuSuDungChiTiet);
                frm.ShowDialog();
            }
        }

        private void grdDichVu_Click(object sender, EventArgs e)
        {
            LoadDmDichVuChiTietSuDung();
        }
        #endregion

        #region "DonThuoc"
        private void isValidDonThuoc()
        {
            var dmDanhSachDonThuocEntity = danhSachDonThuocBusiness.GetDanhSachDonThuoc(txtMaHoSo.Text);
            if (dmDanhSachDonThuocEntity == null)
            {
                isInsertDonThuoc = false;
                txtMaDonThuoc.Text = txtMaHoSo.Text;
            }
            else
            {
                isInsertDonThuoc = true;
                txtMaDonThuoc.Text = dmDanhSachDonThuocEntity.MaDonThuoc;
                txtLoiDanDonThuoc.Text = dmDanhSachDonThuocEntity.LoiDan;
            }
        }

        private void btnLuuDonThuoc_Click(object sender, EventArgs e)
        {
            var danhSachDonThuocEntity = new DanhSachDonThuocEntity()
            {
                MaDonThuoc = txtMaHoSo.Text,
                MaHoSo = txtMaHoSo.Text,
                TenDonThuoc = string.Empty,
                NgayVaoKham = NgayVaoKham,
                MaBacSy = Authenticate.MaBacSy,
                LoiDan = txtLoiDanDonThuoc.Text
            };

            if (isInsertDonThuoc == false)
            {
                danhSachDonThuocBusiness.InsertDanhSachDonThuoc(danhSachDonThuocEntity);
                isInsertDonThuoc = true;
            }
            else
            {
                danhSachDonThuocBusiness.EditDanhSachDonThuoc(danhSachDonThuocEntity);
            }

            MessageBox.Show("Lưu đơn thuốc thành công!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
        }

        private void btnDongDonThuoc_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void LoadDonThuoc(object sender, EventArgs e)
        {
            var lstDonThuocChiTiet = donThuocChiTietBusiness.GetDonThuocChiTietList(txtMaDonThuoc.Text);
            grdDonThuocChiTiet.DataSource = lstDonThuocChiTiet;
        }

        DonThuocChiTietEntity getDonThuocChiTiet()
        {
            if (gvDonThuocChiTiet.RowCount <= 0) return null;
            return (DonThuocChiTietEntity)gvDonThuocChiTiet.GetFocusedRow();
        }

        private void btnThemMoiDonThuocChiTiet_Click(object sender, EventArgs e)
        {
            if (isInsertDonThuoc == false)
            {
                MessageBox.Show("Bạn chưa lưu đơn thuốc!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            var frm = new frmDonThuocChiTiet((int)ConstantManagerClinic.StateInsertOrUpdate.Insert, txtMaDonThuoc.Text);
            frm.donThuocChiTietEvent += new EventHandler(LoadDonThuoc);
            frm.ShowDialog();
        }

        private void btnSuaDonThuocChiTiet_Click(object sender, EventArgs e)
        {
            var donThuocChiTietEntity = getDonThuocChiTiet();
            var frm = new frmDonThuocChiTiet((int)ConstantManagerClinic.StateInsertOrUpdate.Update, donThuocChiTietEntity);
            frm.donThuocChiTietEvent += new EventHandler(LoadDonThuoc);
            frm.ShowDialog();
        }

        private void grdDonThuocChiTiet_DoubleClick(object sender, EventArgs e)
        {
            btnSuaDonThuocChiTiet_Click(sender, e);
        }

        private void btnXoaDonThuocChiTiet_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }

            foreach (DonThuocChiTietEntity dm in deleteObjects)
            {
                donThuocChiTietBusiness.DeleteDonThuocChiTiet(dm.MaVatTu, dm.MaDonThuoc);
            }

            gridCheckMarksSelection.ClearSelection();
            LoadDonThuoc(sender, e);
        }

        #endregion

    }
}